<?php
declare (strict_types=1);

namespace app\course\model\agent;

use app\common\model\BaseModel;
use app\course\model\base\Course;
use app\school\model\school\School;
use think\facade\Event;
use think\model\relation\BelongsTo;
use think\model\concern\SoftDelete;

/**
 * 代理课程
 */
class CourseAgent extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'course_agent';

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'                   => 'integer',
        'status'               => 'integer',
        'mhm_id'               => 'integer',
        'mhm_fullcategorypath' => 'json',
    ];

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 分类数据
     *
     * @param $category
     * @param $data
     * @return string|null
     */
    public function setMhmCategoryAttr($category, $data)
    {
        return $this->setCategoryAttr($category, $data);
    }

    /**
     * 获取类型显示文字
     *
     * @param $value
     * @param $data
     * @return string
     */
    protected function getCourseTypeTextAttr($value = '', $data): string
    {
        $text = [
            1 => '点播',
            2 => '直播',
            3 => '面授',
            4 => '班级',
        ];

        return $text[$data['course_type']];
    }

    /**
     * 机构
     *
     * @return BelongsTo
     */
    public function school(): BelongsTo
    {
        return $this->belongsTo(School::class, 'mhm_id')->with(['level_text', 'logo_url'])->bind(['school_name' => 'title', 'level_text', 'logo_url']);
    }

    /**
     * 代理课程原价
     *
     * @return BelongsTo
     */
    public function costPrice(): BelongsTo
    {
        return $this->belongsTo(Course::class, 'course_id')->with(['cover_url'])->bind(['cost_price' => 'price', 'title', 'cover_url']);
    }
}
